#!/usr/bin/env node
const http=require("http"),
      items=[],
      url=require('url'),
      qs=require('querystring'),
      log=console.log;
//xiugai 
function genPage(){
  console.log(items);
     let  html=`<!DOCTYPE html>
      <html lang="en">
      <head>
       <meta charset="UTF-8">
        <title>登录页面</title>
      </head>
        <body>
        <h1>todo List</h1>
        <form=  method="GET" action="http://192.168.18.144:8080" >
        //提交的方法和提交到的网址
        <input type="text" name="item">
       <input type="submit" value="submit">
      </form>
         <ul id="items">
          ${items.map(i=>'<li>'+i+'</li>').join('\n')}

          </ul>
          </body>
      </html>`;
      return html;
}

     
  http.createServer((req,res)=>{
    let addr=url.parse(req.url);
    console.log(addr.pathname);
    if(addr.pathname!=='/'){
      res.statusCode=404;
      res.setHeader('Content-type','text/html');
      res.end('<h1>Not Found!</h1>');
      return;
    }

     let item=qs.parse(addr.query).item;
     //这里面的item和input属性的name要相同
     console.log(item);
    if(item!== '' && typeof item!=='undefined')items.push(item);
  
    res.writeHead( 200,{
      'Content-type':'text/html',
       'Content-length':Buffer.byteLength(genPage())});
    
     res.end(genPage());
 }).listen(8080)
